import re
import datetime
import pymongo
import common.gyblog as log
import manage_db.query_db as dm
import common.download as download
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService

LOG = log.get_logger()
SERVICE = ChromeService(executable_path="../common/chromedriver.exe")

SELLER = '未性年'
'''
功能：通过FC2卖家名字，下载tor
排除在fc2_manage_record中已记录的，避免重复下载
'''


def get_fc2hub(url):
    # 创建 WebDriver 对象
    driver = webdriver.Chrome(service=SERVICE)
    driver.get(url)

    elements = driver.find_elements(By.XPATH, '//*[@id="content"]/div/div/div[2]/div[5]/div[position()]')
    get_ans = []
    for e in elements:
        fc2_list = re.findall("FC2-PPV-.*?\n", e.text)
        if len(fc2_list) == 1:
            fc2_name = str(fc2_list[0]).strip('\n').replace('PPV-', '')
            get_ans.append(fc2_name)
    print('{}，爬取到{}个番号'.format(url, len(elements)))
    LOG.info('{}，爬取到{}个番号'.format(url, len(elements)))
    driver.close()
    return get_ans


def check_is_exist(get_ans):
    not_exist = []
    # 连接数据库
    db_client = pymongo.MongoClient(dm.CONNECTION_STR)
    db_database = db_client['movies_record']
    db_col = db_database['fc2_manage_record']

    for a in get_ans:
        # 查番号
        query = {'num': a}
        query_ans = list(db_col.find(query))
        if len(query_ans) == 0:
            not_exist.append(a)
    # 打印查询结果
    print('共有{}个没有查询到,他们是：'.format(len(not_exist)))
    LOG.info('共有{}个没有查询到,他们是：'.format(len(not_exist)))
    print(not_exist)
    LOG.info(str(not_exist))

    db_client.close()
    return not_exist


def print_ans(tor_ans):
    print('共有{}个tor：'.format(len(tor_ans)))
    LOG.info('共有{}个tor：'.format(len(tor_ans)))
    log_count = 0
    for tor in tor_ans:
        print(tor)
        LOG.info(tor)
        log_count = log_count + 1
        if log_count % 20 == 0:
            LOG.info('****************************************************************************')


def print_db_query(nums):
    print('共有{}个tor：'.format(len(tor_ans)))
    # 连接数据库
    db_client = pymongo.MongoClient(dm.CONNECTION_STR)
    db_database = db_client[dm.DATA_BASE]
    db_col_record = db_database['fc2_manage_record']
    for tor in tor_ans:
        query_record = {'num': tor}
        query_record_ans = list(db_col_record.find(query_record))

    db_client.close()


def query_url_by_seller(seller):
    # 连接数据库
    db_client = pymongo.MongoClient(dm.CONNECTION_STR)
    db_database = db_client[dm.DATA_BASE]
    db_col_record = db_database['fc2club_url']

    query_record = {'seller': seller}
    query_url_ans = list(db_col_record.find(query_record))
    print(query_url_ans)
    db_client.close()

    return query_url_ans


def update_time(seller):
    # 连接数据库
    db_client = pymongo.MongoClient(dm.CONNECTION_STR)
    db_database = db_client[dm.DATA_BASE]
    db_col_record = db_database['fc2club_url']
    # 获取时间
    now = datetime.datetime.now()
    get_time = now.strftime('%Y-%m-%d %H:%M:%S')
    # 更新数据库
    query = {'seller': seller}
    new_values = {"$set": {"last_time": get_time}}
    db_col_record.update_one(query, new_values)

    db_client.close()


if __name__ == '__main__':
    query_url_ans = query_url_by_seller(SELLER)
    if len(query_url_ans) == 1:
        page = query_url_ans[0]['page']
        for i in range(1, int(page) + 1):
            url = query_url_ans[0]['url'] + '?page={}'.format(str(i))
            get_ans = get_fc2hub(url.format(str(i)))
            not_exist = check_is_exist(get_ans)
            tor_ans = download.download_sukebei_fc2(not_exist)
            print_ans(tor_ans)
        update_time(SELLER)
    else:
        print('没有查询到卖家')
